package com.ycq.parser.attribute;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.function.BiFunction;

/**
 * 属性
 *
 * @author ycq
 * @date 2025/9/24 13:36
 */

public final class AttributeKey<T> {

    /**
     *  答案
     */
    public static final AttributeKey<String> ANSWERS = new AttributeKey<>("答案",(parsedText,attributeNodeMap)->parsedText);

    /**
     * 破题思路
     */
    public static final AttributeKey<String> THINKING = new AttributeKey<>("破题思路",(parsedText,attributeNodeMap)->parsedText);

    /**
     * 解析
     */
    public static final AttributeKey<String> RESOLUTION = new AttributeKey<>("解析",(parsedText,attributeNodeMap)->{
        AttributeNode attributeNode = attributeNodeMap.get(THINKING);
        if(attributeNode == null){
            return parsedText;
        }else{
            return parsedText + "（" + attributeNode.getParsedText() + "）";
        }
    });

    /**
     * 难度系数
     */
    public static final AttributeKey<Integer> DIFFICULTY_LEVEL = new AttributeKey<>("难度系数",(parsedText,attributeNodeMap)->{
        if(StringUtils.isBlank(parsedText)){
            return 1;
        }
        return parsedText.trim().length();
    });

    /**
     * 考频规律
     */
    public static final AttributeKey<Integer> FREQUENCY = new AttributeKey<>("考频规律",(parsedText,attributeNodeMap)->{
        if(StringUtils.isBlank(parsedText)){
            return 1;
        }
        return parsedText.trim().length();
    });


    /**
     * 获取key
     */
    private final String key;

    /**
     * 解析value
     */
    private final BiFunction<String,Map<String,AttributeNode>,T> parseValueFn;

    /**
     * 私有化，不可随意new
     */
    private AttributeKey(String key, BiFunction<String,Map<String,AttributeNode>,T> parseValueFn) {
        this.key = key;
        this.parseValueFn = parseValueFn;
    }

    /**
     *  获取值
     */
    public T get(Map<String, AttributeNode> attributeNodeMap){

        AttributeNode attributeNode = attributeNodeMap.get(key);
        if(attributeNode == null){
            return null;
        }

        String parsedText = attributeNode.getParsedText();
        return parseValueFn.apply(parsedText,attributeNodeMap);
    }



}
